<?php
class App extends Module {
    private $router = array();
    private $site_info = array();


    // --- public functions start 
    public function run() {
        $router = $this->_get_router();
        
        $m = $router['m'];
        $a = 'act_' . $router['a'];
        
        $module = m($m);
        if (method_exists($module, $a)) {
            $module->$a();
            $site_info = $this->_get_site_info();
            out()->add_title($site_info['caption']);
        } else {
            tos_die('error request');
        }
    }


    public function get_base_url() {
        $site_info = $this->_get_site_info();
        $base_url = trim($this->site_info['server_name']);
        $base_path = trim($this->site_info['base_path']);
        if ( ! empty($base_path)) {
            $base_url .= '/' . $base_path;
        }
        return $base_url;
    }
    
    
    public function redirect($router = array(), $protocol = 'http') {
        $url = $this->url($router, $protocol);
        header('Location: ' . $url);
        exit();
    }
    
    
    public function url($router = array(), $protocol = 'http') {
        $query = '';
        if (is_array($router)) {
            $query = $this->_query($router, true);
        }
        $protocol = ($protocol !== 'http') ? 'https' : 'http'; 
        $url = $protocol . '://' . $this->get_base_url();
        if ($query) {
            $url .= '?' . $query;
        }
        return $url;
    }
    
    
    public function get_files($relative_dir) {
        $full_dir = ROOT_DIR . '/' . $relative_dir;
        if ( ! is_dir($full_dir)) {
            tos_dir("cannot find dir $relative_dir .");
        }
        
        $d = dir($full_dir);
        $files = array();
        
        while (false !== ($entry = $d->read())) {
            if ($entry[0] === '.' || $entry[0] === '~') {
                continue;
            }
            $files[] = $entry;
        }
        
        $d->close();
        
        return $files;
    }
    
    
    public function get_tpl_files($relative_dir) {
        $full_dir = ROOT_DIR . '/theme/' . tos_get_config('theme') . '/' . $relative_dir;
        if ( ! is_dir($full_dir)) {
            tos_dir("Cannot find tpl dir $relative_dir.");
        }
        
        $tpl_names = array();
        $d = dir($full_dir);
        
        while (false !== ($entry = $d->read())) {
            if ($entry[0] === '.' || $entry[0] === '~') {
                continue;
            }
            $dot_pos = strpos($entry, '.');
            if ($dot_pos) {
                $tpl_names[] = substr($entry, 0, $dot_pos);
            }
        }
        $d->close();
        
        return $tpl_names;
    }
    // --- public functions end
    
    
    
    // --- private functions start
    private function _get_router() {
        if ( ! $this->router) {
            $site_info = $this->_get_site_info();

            $m = $site_info['module'];
            $a = $site_info['action'];
            $router_id = $site_info['router_id'];

            if ($site_info['level'] !== SITE_LEVEL_LIMIT)  {
                $request_m = tos_http_get('m');
                $request_a = tos_http_get('a');
                
                if ( ! empty($request_m) && ! empty($request_a)) {
                    $m = $request_m;
                    $a = $request_a;
                }
            }
            $router_model = $this->model('router');
            $router_info = $router_model->get_info($m, $a);
            $access_mode = ($router_info && isset($router_info['access_mode'])) ?
                $router_info['access_mode'] : ROUTER_ACCESS_PRIVATE;
            

            if ($access_mode !== ROUTER_ACCESS_PUBLIC) {
                $user_module = m('user');
                if (true !== $user_module->logined()) {
                    $m = 'user';
                    $a = 'login';
                } else if ( ! $user_module->is_admin()) {
                    if ($access_mode === ROUTER_ACCESS_POSITION) {
                        $router_id = $router_info['id'];
                        $position_ids = $user_module->get_positions_ids();
                        if ( ! $router_model->has_position_ids($router_id, $position_ids)) {
                            $m = 'app';
                            $a = 'no_permission';
                        }
                    }
                }
            }

            

            $this->router = array(
                'id'    => $router_id,
                'm'     => $m,
                'a'     => $a
            );
        }
        return $this->router;
    }


    private function _access($router_info) {

    }

    
    private function _get_site_info()  {
        if ( ! $this->site_info) {
            $server_name = $_SERVER['SERVER_NAME'];
            $site_info = $this->model('site')->get_info($server_name);
            $site_info['server_name'] = $server_name;
            $this->_load_site_setting($site_info['name']);
            
            $this->site_info = $site_info;
        }

        return $this->site_info;
    }
    
    
    private function _load_site_setting($site_name) {
        $file = ROOT_DIR . '/site/' . $site_name . '.setting.php';
        if (file_exists($file)) {
            include_once($file);
        }
    }
    
    
    function _query($ps, $is_new = false) {
        
        $arr = array();
        if ( ! $is_new) {
            $ps = array_merge($_GET, $ps);
        }
        foreach ($ps as $k => $v) {
            $k = trim($k);
            $v = trim($v);
            if ( ! empty($k) && ! empty($v)) {
                $arr[] = "$k=$v";
            }
        }
        $str = implode('&', $arr);
        
        return $str;
    }
    // --- private functions end
}
?>